package com.example.evanzeng.viewpagertest;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.evanzeng.viewpagertest.ViewPager.MyTransformation;

import java.util.ArrayList;
import java.util.List;

public class MainActivityTest extends AppCompatActivity {

    private int pagerWidth;
    private List<ImageView> imageViewList;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);



        imageViewList = new ArrayList<>();
        ImageView first = new ImageView(MainActivityTest.this);
//        setWh(first);
        /**
         * 为imageview生成的带犹豫倒影的bitmap
         */
//        first.setImageBitmap(ImageUtil.getReverseBitmapById(R.mipmap.first,MainActivityTest.this));
//        ImageView second=new ImageView(MainActivityTest.this);
//        second.setImageBitmap(ImageUtil.getReverseBitmapById(R.mipmap.second,MainActivityTest.this));
//        ImageView third=new ImageView(MainActivityTest.this);
//        third.setImageBitmap(ImageUtil.getReverseBitmapById(R.mipmap.third,MainActivityTest.this));
//        ImageView fourth=new ImageView(MainActivityTest.this);
//        fourth.setImageBitmap(ImageUtil.getReverseBitmapById(R.mipmap.fourth,MainActivityTest.this));
//        ImageView fifth=new ImageView(MainActivityTest.this);
//        fifth.setImageBitmap(ImageUtil.getReverseBitmapById(R.mipmap.fifth,MainActivityTest.this));

        first.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.first));

        ImageView second = new ImageView(MainActivityTest.this);
        second.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.second));
//        setWh(second);

        ImageView third = new ImageView(MainActivityTest.this);
        third.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.third));
//        setWh(third);

        ImageView fourth = new ImageView(MainActivityTest.this);
        fourth.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.fourth));

//        setWh(fourth);

        ImageView fifth = new ImageView(MainActivityTest.this);
        fifth.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.s6));
//        setWh(fifth);

        ImageView s6 = new ImageView(MainActivityTest.this);
        s6.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.s7));
//        setWh(s6);

        ImageView s7 = new ImageView(MainActivityTest.this);
        s7.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.s8));
//        setWh(s7);

        ImageView s8 = new ImageView(MainActivityTest.this);
        s8.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.s9));
//        setWh(s8);

        ImageView s9 = new ImageView(MainActivityTest.this);
        s9.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.s11));
//        setWh(s9);
        imageViewList.add(first);
        imageViewList.add(second);
        imageViewList.add(third);
        imageViewList.add(fourth);
        imageViewList.add(fifth);
        imageViewList.add(s6);
        imageViewList.add(s7);
        imageViewList.add(s8);
        imageViewList.add(s9);


        viewPager.setOffscreenPageLimit(3);
        pagerWidth = (int) (getResources().getDisplayMetrics().widthPixels * 3.0f / 5.0f);
        ViewGroup.LayoutParams lp = viewPager.getLayoutParams();
        if (lp == null) {
            lp = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            lp.width = pagerWidth;
        }
        viewPager.setLayoutParams(lp);
        viewPager.setPageMargin(-10);
        findViewById(R.id.activity_main).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return viewPager.dispatchTouchEvent(motionEvent);
            }
        });
        viewPager.setPageTransformer(true, new MyTransformation());
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imageViewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(imageViewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = imageViewList.get(position);
//                setWh(imageView);
                container.addView(imageView);
                return imageView;
            }
        });

    }

    private void setWh(ImageView imageView) {

        //设置图片参数
        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        layoutParams.width = getResources().getDimensionPixelOffset(R.dimen.sw_dp_225);
        layoutParams.height = getResources().getDimensionPixelOffset(R.dimen.sw_dp_438);
//        if(layoutParams==null){
//            layoutParams = new ViewGroup.LayoutParams();
//        }

        imageView.setLayoutParams(layoutParams);
    }


}
